package com.linln.admin.system.web;

import com.github.pagehelper.Page;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.entity.ActivityExpand;
import com.linln.modules.system.entity.BeautyCompany;
import com.linln.modules.system.entity.UserExpand;
import com.linln.modules.system.entity.WxUser;
import com.linln.modules.system.service.ShareServices;
import com.linln.modules.system.service.WxUserServices;
import com.linln.modules.system.vo.ExpandMap;
import com.linln.modules.system.vo.UserExpandNumVo;
import com.linln.modules.system.vo.UserExpandVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/mains/share")
public class ShareController extends BaseController{

    @Autowired
    private ShareServices wxUserServices;

    @RequestMapping(value = "/index")
    @RequiresPermissions("mains:share:index")
    public ModelAndView getIndex(Integer pageNum) {
        Integer pageIndex = HttpServletUtil.getParameterInt("size", 1);
        pageNum = pageNum == null ? super.pageNum : pageNum;
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/share/index");
        Page<ActivityExpand> company = wxUserServices.getUserPager(pageNum, pageIndex*10);
        view.addObject("list", company.getResult());
        view.addObject("page", company);
        return view;
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    public String toAdd() {
        return "/mains/share/add";
    }


    @ResponseBody
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ResultVo saveShare(String remark, String varpath, String params) {
        ResultVo vo = ResultVoUtil.error("操作失败");
        try {
            int end = wxUserServices.saveShare(remark, varpath, params);
            if (end > 0) {
                vo.setCode(200);
                vo.setMsg("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }


    @RequestMapping(value = "detail/{id}")
    public ModelAndView getDetail(@PathVariable("id") String id){
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/share/detail");
        ExpandMap map = wxUserServices.getExpandMap(id);
        List<UserExpandVo> list = map.getVo1();
        List<UserExpandNumVo> nums = map.getVo2();
        view.addObject("aid",id);
        view.addObject("list",list);
        view.addObject("nums",nums);
        return view;
    }

    @ResponseBody
    @RequestMapping(value = "moreDetail")
    public List<UserExpandVo> getTimeSearch(String id,String startTime,Integer auth){
        List<UserExpandVo> vos = wxUserServices.getActivityExpand(id, startTime,auth);
        return vos;
    }

}
